/////////////////////////
// 	CBScroll.cpp	:	v0010
//	Written by 		:	Li	Haijun
//	Compiler		:	Microsoft Visual C++ 4.2
//	v0010			:	Dec.24.1996
/////////////////////////
#include	"stdafx.h"
#include	<stdio.h>

#include	"DDCompo.h"
#include	"Assert.h"
#include	"DDButton.h"
#include	"Marco.h"
#include	"DDScroll.h"

////////////////////////////
#include "l_allbmp.h"
extern char	bmpfilename[30];
extern char	listfilename[30];
/////////////////////////////////////

extern class	CDDButton	*	pAllButton[];	//to restore all button's pointer
/////////////////////////////////////////////////
//	to restore all scroll-bar's pointer
class	CDDScrollBar	*	pAllScrollBar[MAX_SCROLLBAR];
////////////////////////////////////////////////////
// major parameter for bitmap(it record the current state of operate)
class	CDDScrollBar *	pCurrentScrollBar = NULL;	// a pointer point to the current scroll-bar
int		nCurrentScrollBarState = NONE;	// current scroll-bar's state,-10-none,0-up,1-focus,2-disable

/////////////////////////////////////////////////
//	construct function
CDDScrollBar::CDDScrollBar()
{
	m_nTotalPart = 0;
	m_nCurrentPart = 0;
	m_nFileNameId = NONE;
	m_szOneBitmap.cx = 0L;
	m_szOneBitmap.cy = 0L;

	m_rTotal.left	= 0L;
	m_rTotal.top	= 0L;
	m_rTotal.right	= 0L;
	m_rTotal.bottom	= 0L;

	m_rCurrent.left	= 0L;
	m_rCurrent.top	= 0L;
	m_rCurrent.right	= 0L;
	m_rCurrent.bottom	= 0L;

	m_dwX = 0;	m_dwY = 0;
	m_nCurrentPart = 0;		// the percent of this scroll-bar
	m_nTotalPart = 0;		// how many part was this scroll-bar divided

	m_nID = 0;
	m_nState = SCROLLBAR_UP;
}
/////////////////////////////////////////////////
//	disconstruct function
CDDScrollBar::~CDDScrollBar( )
{
	Release();
	m_nFileNameId = NONE;
}
BOOL CDDScrollBar::PreLoad(int FilenameId, int x, int y,
						int OneBitmapWidth/*=0*/, int OneBitmapHeight/*=0*/)
{
	char	fname[20];

	HRESULT			ddrval;
	DDSURFACEDESC	ddsd;

//////////////////////////////////////////////////
	_itoa( FilenameId, fname, 10 );

	class CPicture_imageall picture;

	picture.image_open_compress(bmpfilename);
	picture.image_open_index(listfilename);
	picture.LoadBitmap( &m_BitmapSurface, FilenameId );
	picture.image_close_index();
	picture.image_close_compress();

/////////////////////////////////////////////////////////
	// to set the value of m_FileName
	m_nFileNameId = FilenameId;	//set the member veriable m_FileName
    //
    // get size of surface.
    //
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;

    ddrval = m_BitmapSurface.GetSurface()->GetSurfaceDesc(&ddsd);
	if(ddrval != DD_OK)
		return FALSE;
	m_rTotal.left = m_rTotal.top = 0;
	m_rTotal.right = ddsd.dwWidth;
	m_rTotal.bottom = ddsd.dwHeight;
// to set the value of m_szOneBitmap
	if( (OneBitmapWidth==0)&&(OneBitmapHeight==0) )
	{
		// I assume that all single bitmaps' height is equal to it's width
		if(m_rTotal.bottom > m_rTotal.right)
			m_szOneBitmap.cx = m_szOneBitmap.cy = m_rTotal.right;
		else
			m_szOneBitmap.cx = m_szOneBitmap.cy = m_rTotal.bottom;
		// to set the value of m_rCurrent
		m_rCurrent.left = m_rCurrent.top = 0;
		m_rCurrent.right = m_rCurrent.bottom = m_szOneBitmap.cx;
	}
	else if( OneBitmapWidth * OneBitmapHeight == 0 )
		return FALSE;
	else
	{
		// I assume that all single bitmaps' height is not equal to it's width
		m_szOneBitmap.cx = OneBitmapWidth;
		m_szOneBitmap.cy = OneBitmapHeight;
	}
//to set the value of m_dwX, m_dwY
	m_dwX = x; m_dwY = y;
	SetState(SCROLLBAR_UP);
// to preload the Left-arrow and right arrow as buttons
//	m_nLeftArrow.PreLoad("bmp\\sarrow_l.bmp", x-19, y+5, 15, 16, TRUE );
	m_nLeftArrow.PreLoad(269, x-19, y+5, 15, 16, TRUE );
	m_nLeftArrow.SetState( BUTTON_UP );
//	m_nRightArrow.PreLoad("bmp\\sarrow_r.bmp", x+m_szOneBitmap.cx+4, y+5, 15, 16, TRUE);
	m_nRightArrow.PreLoad(270, x+m_szOneBitmap.cx+4, y+5, 15, 16, TRUE);
	m_nRightArrow.SetState( BUTTON_UP );
//	m_BitmapScrollBlock.PreLoad("bmp\\scrol_b.bmp", 0, 0, 44, 12);
	m_BitmapScrollBlock.PreLoad(271, 0, 0, 44, 12);

	return TRUE;
}
// to set the percent of this scroll bar
void CDDScrollBar::SetCurrentPart(int part)
{
	int PosX = 0;

	m_nCurrentPart = part;
//	PosX = (m_dwX +5) + m_nCurrentPart*( ( (m_szOneBitmap.cx-10) - m_nLeftArrow.GetSize().cx)/m_nTotalPart);
//	PosX = (m_dwX +5);// + m_BitmapScrollBlock.GetSize().cx/2;// + m_nCurrentPart*(m_szOneBitmap.cx - 10 - m_BitmapScrollBlock.GetSize().cx)/m_nTotalPart;
	PosX = (m_dwX +5) + m_nCurrentPart*(m_szOneBitmap.cx - 10 - m_BitmapScrollBlock.GetSize().cx)/m_nTotalPart;
	m_BitmapScrollBlock.SetPosition(PosX, m_dwY+7);
//	Show();
}
// to get the percent of this scroll bar
int	CDDScrollBar::GetCurrentPart(void)
{
	return m_nCurrentPart;
}
// to set the id of this scroll-bar
void CDDScrollBar::SetID(int ID)
{
	m_nID = ID ;	// button id
	return ;
}
// to set the state of this scroll-bar
void	CDDScrollBar::SetState( int state/*=-1*/ )
{
	m_nState = state;

	if( m_szOneBitmap.cx == m_rTotal.right )
	{
		m_rCurrent.left = m_rTotal.left;
		m_rCurrent.top = m_rTotal.top + m_nState * m_szOneBitmap.cy;
		m_rCurrent.right = m_rCurrent.left + m_szOneBitmap.cx;
		m_rCurrent.bottom = m_rCurrent.top + m_szOneBitmap.cy;
	}
	else
	{
		m_rCurrent.left = m_rTotal.left + m_nState * m_szOneBitmap.cx;
		m_rCurrent.top = m_rTotal.top;
		m_rCurrent.right = m_rCurrent.left + m_szOneBitmap.cx;
		m_rCurrent.bottom = m_rCurrent.top + m_szOneBitmap.cy;
	}
	
	if( state == SCROLLBAR_DISABLE )
	{
		m_BitmapScrollBlock.SetState(0);
		m_nLeftArrow.SetState(BUTTON_DISABLE);
		m_nRightArrow.SetState(BUTTON_DISABLE);
	}
	else if( state == SCROLLBAR_UP )
	{
		m_BitmapScrollBlock.SetState(0);
		m_nLeftArrow.SetState(BUTTON_UP);
		m_nRightArrow.SetState(BUTTON_UP);
	}
/*	else if( state == SCROLLBAR_FOCUS )
	{
		m_nLeftArrow.SetState(BUTTON_FOCUS);
		m_nRightArrow.SetState(BUTTON_FOCUS);
	}
*/	return;
}
//to get the state of this scroll-bar 
int		CDDScrollBar::GetState()
{
	return m_nState;
}
//to test whether the given point is on this scroll-bar
BOOL CDDScrollBar::OnScrollBar( int x, int y)
{
	int	m_nLeftEdge = m_dwX - (int)m_nLeftArrow.GetSize().cx - 4;
	int m_nRightEdge = m_dwX + (int)m_szOneBitmap.cx + (int)m_nRightArrow.GetSize().cx + 4;
	int m_nTopEdge = m_dwY;
	int m_nBottomEdge = m_nTopEdge + (int)m_szOneBitmap.cy;

	if( (x >= m_nLeftEdge)&&(x <= m_nRightEdge )
	  &&(y >= m_nTopEdge )&&(y <= m_nBottomEdge) )
		return TRUE;
	else
		return FALSE;
}
// to show this scroll-bar
void CDDScrollBar::Show(int flag /*= 0*/)	//if flag=FIRST -->to show this scroll-bar first time
										//if flag=0		-->to show the middle-bar only
{
	Blit( flag );
	Update( flag );
}
// to blit this scrollbar to back buffer
void CDDScrollBar::Blit(int flag/*= 0*/) //if flag=FIRST -->to show this scroll-bar first time
										 //if flag=0	-->to show the middle-bar only

{
	POINT	ptDest;

	ptDest.x = m_dwX;
	ptDest.y = m_dwY;
	
	m_BitmapSurface.BltToBack(ptDest, &m_rCurrent);
	
	// to blit the scroll-block that belong to this scroll-bar
	m_BitmapScrollBlock.Blit();
	// to blit left arrow and right arrow when you show this scroll-bar first time
	if( flag == FIRST )
	{
	// to blit the left-arrow, right-arrow
		m_nLeftArrow.Blit();
		m_nRightArrow.Blit();
	}
}
// to update the area of this scrollbar stay
void CDDScrollBar::Update(int flag /*= 0*/)	//if flag=FIRST -->to show this scroll-bar first time
											//if flag=0		-->to show the middle-bar only
{
	RECT	ShowRect;
	ShowRect.left = m_dwX,	ShowRect.top = m_dwY;
	ShowRect.right = m_dwX + m_szOneBitmap.cx;
	ShowRect.bottom = m_dwY + m_szOneBitmap.cy;
	DDC_UpdateScreen( &ShowRect );
	// to show the scroll-block that belong to this scroll-bar
//	m_BitmapScrollBlock.Update();
	// to show left arrow and right arrow when you show this scroll-bar first time
	if( flag == FIRST )
	{
	// to show the left-arrow, right-arrow
		m_nLeftArrow.Blit();
		m_nRightArrow.Blit();
	}
}
// to release this scrollbar's pointer
void CDDScrollBar::Release()
{
	// release the surface that this button's bitmap used
	m_BitmapSurface.Release();
	m_BitmapScrollBlock.Release();

	for(int i=0; i<MAX_BUTTON; i++)
	{
		if( pAllButton[i] != NULL )
		{
			if( pAllButton[i] == &m_nLeftArrow )
			{
				pAllButton[i] = NULL;
				m_nLeftArrow.Release();
			}
			else
			{ if( pAllButton[i] == &m_nRightArrow )
				{
					pAllButton[i] = NULL;
					m_nRightArrow.Release();
				}
			}
		}
	}
}
/////////////////////////////////////////
// to test whether a scroll-bar is clicked --- WM_LBUTTONDOWN
/////////////////////////////////////////
BOOL	LeftButtonDownOnScrollBar(int xPos, int yPos)
{
	for( int i=0; i<MAX_SCROLLBAR; i++ )
	{
		if( (pAllScrollBar[i] != NULL)
		  &&(pAllScrollBar[i]->GetState() != SCROLLBAR_DISABLE)
		  &&(pAllScrollBar[i]->OnScrollBar(xPos, yPos)) )
		{
			// to restore the other focus scroll-bar to up state
			for(int j=0; j<MAX_SCROLLBAR ; j++)
			{
				if( (pAllScrollBar[j] != NULL)
				  &&(pAllScrollBar[j]->GetState() == SCROLLBAR_FOCUS) )
				{
					// set the just been clicked button's state
					pAllScrollBar[j]->SetState(SCROLLBAR_UP);
					pAllScrollBar[j]->Show();
				}
			}
			// set the just been clicked button's state
			pAllScrollBar[i]->SetState(SCROLLBAR_FOCUS);
			pAllScrollBar[i]->Show( );
			
			if( pAllScrollBar[i]->m_nLeftArrow.OnButton(xPos, yPos) )
			{
				int part = pAllScrollBar[i]->GetCurrentPart();
				part = part - 1;
				if( part < 0 )
					part = 0;
				pAllScrollBar[i]->SetCurrentPart( part );
				pAllScrollBar[i]->Show();
			}
			else if( pAllScrollBar[i]->m_nRightArrow.OnButton(xPos, yPos)	)
			{
				int part = pAllScrollBar[i]->GetCurrentPart();
				part = part + 1;
				if( part > pAllScrollBar[i]->GetTotalPart() )
					part = pAllScrollBar[i]->GetTotalPart();
				pAllScrollBar[i]->SetCurrentPart( part );
				pAllScrollBar[i]->Show();
			}
			else if( (xPos > (pAllScrollBar[i]->m_dwX + 5) )
				&&(xPos < (pAllScrollBar[i]->m_dwX + pAllScrollBar[i]->m_szOneBitmap.cx - 5) ) )
			{
				int	PartLength = 0;
				int TotalLength = 0;
				int	CurrentPart = 0;

				int BlockLength = pAllScrollBar[i]->m_BitmapScrollBlock.GetSize().cx;

				PartLength = xPos - (pAllScrollBar[i]->m_dwX + 5);
				TotalLength = pAllScrollBar[i]->m_szOneBitmap.cx - 10 ;

				int CurrentPercent = PartLength*100/TotalLength ;
				int EveryPartPercent = 100/pAllScrollBar[i]->m_nTotalPart;

				CurrentPart = CurrentPercent / EveryPartPercent ;
				if( (CurrentPercent - EveryPartPercent * CurrentPart) > 5 )
					CurrentPart ++ ;
				pAllScrollBar[i]->SetCurrentPart( CurrentPart );
				pAllScrollBar[i]->Show();
			}
			// to set the global button's state
			pCurrentScrollBar = pAllScrollBar[i];
			nCurrentScrollBarState = pAllScrollBar[i]->GetState();
			return TRUE;
		}
	}	
	pCurrentScrollBar = NULL;
	return FALSE;
}
/////////////////////////////////////////
// to delete all scroll-bar in the volume of pAllScrollBar[] array
void	FACE_DeleteAllScrollBar(void)
{
	int i;
	for(i=0; i<MAX_SCROLLBAR; i++)
	{
		if( pAllScrollBar[i] != NULL )
		{
			delete pAllScrollBar[i];
			pAllScrollBar[i] = NULL;
		}
	}
}
////////////////////////////////////////////
void	FACE_BlitAllScrollBar(void)
{
	int i;
	for(i=0; i<MAX_SCROLLBAR; i++)
	{
		if( pAllScrollBar[i] != NULL )
		{
			pAllScrollBar[i]->Blit() ;
		}
	}
}
/////////////////////////////////////////////